﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrarySentRecvMessageSynWorkBase;
using ClassLibraryMessageDispacher;
using System.Windows.Forms;
namespace GUI
{

    public delegate void FunctionNotifyComStr_Event(string sender);
    public delegate void FunctionUpdataComMes_Event(ref MessagePackage mes);
    public class GUICommunicationClass
    {
        public static readonly GUICommunicationClass instance = new GUICommunicationClass();

        private ClassMessageSynWork GUICommunicateComSynWorkObj = null;

        public FunctionNotifyComStr_Event FunctionNotifyComStr_event = null;

        public FunctionUpdataComMes_Event FunctionUpdataComMes_event = null;

       private GUICommunicationClass()
        {
            GUICommunicateComSynWorkObj = new ClassMessageSynWork();
            if(ClassMessagerDispacher.instance.isInitInstance)
            {
                ClassMessagerDispacher.instance.uimesOut = GUICommunicateComSynWorkObj.pushMessage_FunctionMessageOutCom;//调度器 塞入发到UI的消息接口
                GUICommunicateComSynWorkObj.notify_event_func = FunctionNotifyComStr;
                GUICommunicateComSynWorkObj.updatapush_event_func = FunctionUpdataPushCom;
            }
            else
            {

            }
        }

      

        public ushort GetSquence()
        {
            return ClassMessagerDispacher.instance.GetSquence();
        }
        public bool Sent2DBbasevMessage(ref MessagePackage sendMes, out MessagePackage outMes)
        {
            outMes = null;
            try
            {

                return GUICommunicationClass.instance.GUICommunicateComSynWorkObj.Send(ref sendMes, out outMes);

            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool Sent2MiddleDevMessage(ref MessagePackage sendMes, out MessagePackage outMes)
        {
            outMes = null;
            try
            {
              
                return GUICommunicationClass.instance.GUICommunicateComSynWorkObj.Send(ref sendMes, out outMes);

            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public  bool Sent2MiddleDevCreat( string  destIP, ushort squence, ref byte[]  indataArry,out MessagePackage outMes)
        {
            outMes = null;
            try
            {
                if (indataArry[1] != 0x00) { throw new Exception("direct err"); }
                ArraySegment<byte> buf1 = new ArraySegment<byte>(indataArry);
                SoureDetailInfo soinfo = new SoureDetailInfo();
                soinfo.sourepath = MessagePackagePath.UICom;
                DestDetailInfo deinfo = new DestDetailInfo();
                deinfo.destpath = MessagePackagePath.NetMiddleDeviceCom;
                deinfo.destIP = destIP;
                deinfo.DB = "";
                MessagePackage mes1 = new MessagePackage(  
                    ProductType.request,
                    0x00,
                    squence,                 
                    true,
                    MessagePackagePath.UICom,
                    MessagePackagePath.NetMiddleDeviceCom,
                    soinfo,
                    deinfo,
                    "",
                    buf1);
                return GUICommunicationClass.instance.GUICommunicateComSynWorkObj.Send(ref mes1, out outMes);
            
            }catch(Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 对接动作事件通知函数
        /// </summary>
        /// <param name="str"></param>
        private void FunctionNotifyComStr(ref string str)
        { 
          
            if (FunctionNotifyComStr_event != null)
            {
                FunctionNotifyComStr_event(str);
            }
         
        }
        /// <summary>
        /// 对接方主推数据回调
        /// </summary>
        /// <param name="str"></param>
        private void FunctionUpdataPushCom(ref MessagePackage mes)
        {
            if (FunctionUpdataComMes_event != null) FunctionUpdataComMes_event(ref mes);
        }

    }
}
